from PyQt4.QtCore import *
from PyQt4.QtGui import *
from zmq import PUSH, PULL
import random
import time
from sys import argv

from zmqsocket import *

class Worker(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        _title="Worker %s" % argv[1] if len(argv)==2 else "Worker"
        self.setWindowTitle(_title)

        QVBoxLayout(self)
        self.trace=QTextEdit(self)
        self.trace.setReadOnly(True)
        self.layout().addWidget(self.trace)

        self.receiver=ZmqSocket(PULL, self)
        self.receiver.readyRead.connect(self.read)
        self.receiver.connect("tcp://localhost:5557")

        self.sender=ZmqSocket(PUSH, self)
        self.sender.connect("tcp://localhost:5558")

    def read(self):
        s=self.receiver.recv()
        for _s in s:
            self.trace.append(" %s" % _s)
            time.sleep(int(_s)*0.001)
            self.sender.send(_s)
        
if __name__=="__main__":
    a=QApplication(argv)
    w=Worker()
    w.show()
    exit(a.exec_())
